﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity = VietStar.Library.Entity;
using Business = VietStar.Library.Business;
using VietStar.Library.Commom.Module;
public partial class controls_client_detailproduct : System.Web.UI.UserControl
{
    Entity.SanPham clsE_SanPham = new VietStar.Library.Entity.SanPham();
    Business.SanPham clsB_SanPham = new VietStar.Library.Business.SanPham();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            clsE_SanPham.Id= Convert.ToInt32(Request.QueryString["key"].ToString());
            clsB_SanPham.UpdateLuotXem(clsE_SanPham);
            Page.Title = clsB_SanPham.SelectPrimarykey(clsE_SanPham).Rows[0]["TenSanPham"].ToString(); ;
        }

    }

    protected void list_ItemCommand(object source, DataListCommandEventArgs e)
    {
        int index = e.Item.ItemIndex;
        int key = Convert.ToInt32(list.DataKeys[index].ToString());
        clsE_SanPham.Id = key;
        if (e.CommandName.Equals("buy"))
        {
            DataTable dt = clsB_SanPham.SelectPrimarykey(clsE_SanPham);
            if (dt.Rows.Count > 0)
            {
                Cart cart = (Cart)Session["cart"];
                if (cart.AddItem(key, dt.Rows[0]["TenSanPham"].ToString(), Convert.ToDouble(dt.Rows[0]["DonGia"])))
                {
                    //update cartviewer
                    Control control = Page.Form.FindControl("yourcart");
                    Label count = (Label)FindControls.Find("count", control);
                    Label price = (Label)FindControls.Find("price", control);
                    if (count != null && count != null)
                    {
                        count.Text = cart.CartData.Count + " sản phẩm";
                        price.Text = cart.TotalPrice.ToString("N0") + " vnđ";
                    }
                }
            }
        }
    }
}
